package CutLoad.components.window
{
	import CutLoad.Base;
	import CutLoad.components.CButton;
	import CutLoad.components.Skin;
	import CutLoad.components.container.CScrollV;
	import CutLoad.components.container.ContainerH;
	import CutLoad.cookies.Fun;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * 分页窗口 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CTabbed extends Base
	{
		private var arrSkinConfig:Array;
		private var win:CWindow;
		private var tabBoder:Sprite,tabBarContainer:ContainerH;
		private var tabBarContainer_spaceX:Number,tabBarContainer_spaceY:Number;
		//
		private var scrollContainer:CScrollV;
		//
		private var funChang:Function;
		//
		//分页按钮标签数组,当前页,分页内容数组
		private var arrTabBtns:Array,currentIndex:Number,arrContent:Array;
		/**
		 * 初始化分页窗口 
		 * @param tile 窗口标题目
		 * @param arrTabTitle 分页按钮标签:格式：[按钮1标签,按钮2标签,按钮3标签,按钮4标签....]
		 * @param arrContent 要显示的内容数组，必须与“分页按钮标签数组长度”是一样的。
		 * @param ww 宽度
		 * @param hh 高度
		 * @param funChang 点击分页时调用此方法:格式：function(当前分页窗口类:CTabbed)
		 * @param showOne 初始化时显示那个标签,默为0，显示第一个
		 * @param isResizContentWH 分页窗口容器里的内容是否随，窗口的尺寸的改变而改变，默认为不改变
		 * 
		 */		
		public function init(tile:String,arrTabTitle:Array,arrContent:Array,ww:Number,hh:Number,funChang:Function=null,showOne:int=0,isResizContentWH:Boolean=false):void{
			if(arrTabTitle.length!=arrContent.length) throw new Error("这两个参数数组的长度要一样：arrTabTitle，arrContent");
			if(showOne<0||showOne>arrTabTitle.length-1) throw new Error("参数：showOne的值已过可以显示的分页数范围");
			//
			this.arrContent=arrContent;
			this.funChang=funChang;
			if(arrSkinConfig==null) arrSkinConfig=Skin.getInstance().getSkin(8);
			win=new CWindow();
			win.init(tile,true,true,false,ww,hh,this);
			//排出tabbar
			tabBarContainer=new ContainerH();
			var spaceY:Number=4;
			var spaceX:Number=5
			tabBarContainer_spaceX=spaceX;
			tabBarContainer_spaceY=spaceY;
			tabBarContainer.init({w:10,h:10,isAuto:false,xspace:spaceY,yspace:spaceX});
			var btn:CButton;
			if(showOne<0||showOne>arrTabTitle.length-1) throw new Error("参数错误!只能:0~"+String(arrTabTitle.length-1));
			currentIndex=showOne
			for(var i:Number=0;i<arrTabTitle.length;i++){
				btn=getTabbedBtn(arrTabTitle[i])
				btn.setOtherInfo({tabbedBarId:i})
				btn.addEventListener(MouseEvent.CLICK,clickBar);
				tabBarContainer.append(btn);
			}
			tabBarContainer.setTakeWH();
			//tabBarContainer.showGuide();
			//
			tabBoder=Base.getStrClassObject(arrSkinConfig[4]) as Sprite;
			//
			tabBoder.x=tabBarContainer.x+spaceX;
			tabBoder.y=tabBarContainer.getShowHeight()-spaceY-arrSkinConfig[5];
			//
			win.getContainer().append(tabBarContainer);
			tabBarContainer.getChildeParent().addChild(tabBoder);
			//获得当前选择重的分页，并设置它在边框的上面
			arrTabBtns=tabBarContainer.getHChildeParent().data
			var btnSelect:CButton=arrTabBtns[currentIndex] as CButton;
			Fun.setTopDepath(btnSelect);
			//
			scrollContainer=new CScrollV(arrContent[currentIndex],100,100,0.3,true,2,isResizContentWH);
			scrollContainer.setXY(tabBoder.x+3,tabBoder.y+3,tabBarContainer.getChildeParent());
			//
			setWHBorder()
			//
			win.setXY(0,0,this);
		}
		/**
		 * 分页窗口容器里的内容是否随窗口的尺寸的改变而改变
		 * @param isChangBody
		 * 
		 */		
		public function set resizeBodyWH(isChangBody:Boolean):void{
			this.scroll.resizeBodyWH=isChangBody;
		}
		public function get resizeBodyWH():Boolean{
			return this.scroll.resizeBodyWH;
		}
		/**
		 * 获得滚动条容器 
		 * @return 
		 * 
		 */		
		public function get scroll():CScrollV{
			return scrollContainer;
		}
		/**
		 * 设置显示的内容 
		 * @param content
		 * 
		 */		
		public function setContent(content:Base):void{
			arrContent[currentIndex]=content
			scroll.resetBody(content);
		}
		/**
		 * 获得分页窗口的窗口对象 
		 * @return 
		 * 
		 */		
		public function get window():CWindow{
			return win;
		}
		/**
		 * 设置要切换的分页 
		 * @param index 分页索引，注：0开始
		 * 
		 */		
		public function set value(index:Number):void{
			if(currentIndex==index) return;
			if(index<0||index>arrTabBtns.length-1) throw new Error("参数错误!只能:0~"+String(arrTabBtns.length-1));
			var btnSelect:CButton=arrTabBtns[index] as CButton;
			var CurrentSelect:CButton=arrTabBtns[currentIndex] as CButton;
			CurrentSelect.parent.swapChildren(btnSelect,CurrentSelect);
			currentIndex=index;
		}
		/**
		 * 获得当前切换分页的索引 
		 * @return 
		 * 
		 */		
		public function get value():Number{
			return currentIndex;
		}
		//更新边框大小
		private function setWHBorder():void{
			tabBoder.width=(win.getContainer().getShowWidth()-tabBarContainer_spaceX*2);
			tabBoder.height=(win.getContainer().getShowHeight()-tabBarContainer.getShowHeight()-tabBarContainer_spaceY);
			scrollContainer.setWH(tabBoder.width-6,tabBoder.height-6);
		}
		public function setSkin(arrSkinConfig:Array):void{
			this.arrSkinConfig=arrSkinConfig.tabbedBarId;
		}
		override public function setWH(w:Number=0, h:Number=0):void{
			win.setWH(w,h);
			setWHBorder();
		}
		//改变分页
		private var funChang_for_save_value:Number=0;
		private function clickBar(e:MouseEvent):void{
			var cbtn:CButton=e.target as CButton;
			var btnid:Number=cbtn.getOtherInfo().tabbedBarId
			value=btnid;
			//同一个地方，不用重复
			if(funChang_for_save_value!=value){
				funChang_for_save_value=value;
				//
				//
				scroll.resetBody(arrContent[currentIndex]);
				//
				if(funChang!=null) funChang(this)
			}
		}
		private function getTabbedBtn(strLabel:String):CButton{
			var btn:CButton=new CButton();
			var arrSkin:Array=arrSkinConfig;
			var arrBtnSkin:Array;
			arrBtnSkin=[arrSkin[0],arrSkin[1],arrSkin[2],arrSkin[3]]
			btn.setSkin(arrBtnSkin,strLabel);
			btn.init(strLabel);
			return btn;
		}
	}
}
